package word;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URLDecoder;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import com.attilax.com.ComTask;
import com.attilax.ex.TimeoutExceptionRuntime;
import com.attilax.office.Com_jacob_office_Util;
import com.attilax.office.HttpHandler_servletWrapper_task;


//  word.WordServer
public class WordServer  {

	static WordServer instance;
	
	int port =8888;

	private WordServer() {
		try {
			port = Integer.parseInt(System.getProperty("portx"));
		} catch (NumberFormatException e) {
			throw new RuntimeException("should set -Dportx");
		}
		
		System.out.println("");

		// ini
//		word_ActiveXComponent = new ActiveXComponent("Word.Application");
//		word_ActiveXComponent.setProperty("Visible", new Variant(true)); // ���ɼ���word
//		word_ActiveXComponent.setProperty("AutomationSecurity", new Variant(3)); // ���ú�
//
//		powerpoint_ActiveXComponent = new ActiveXComponent("Powerpoint.Application");
//		powerpoint_ActiveXComponent.setProperty("Visible", new Variant(true)); // ���ɼ���ppt
//		// powerpoint.setProperty("AutomationSecurity", new Variant(3)); //���ú�
//
//		// //���documents���� get doc obj.
//		doc_Dispatch = word_ActiveXComponent.getProperty("Documents").toDispatch();
//		ppt_Dispatch = powerpoint_ActiveXComponent.getProperty("Presentations").toDispatch();
	}

	public WordServer(int no_ini) {

	}

	public static WordServer getInstance() {
		if (WordServer.instance == null) {
			WordServer.instance = new WordServer();
		}
		return WordServer.instance;
	}





	// ���� already ini ed...
	public void run() {

		System.out.println("---f");

		ServerSocket ServerSocket1;

		try {
			ServerSocket1 = new ServerSocket(port);
		} catch (IOException e1) {
			throw new RuntimeException(e1);
		}
		System.out.println(".   .start with port:" + String.valueOf(port));
		while (true) {
			try {
				Socket socket =    ServerSocket1.accept();
				//whileItem(ServerSocket1);
				 new HttpHandler_servletWrapper_task(socket).start();
			} catch (Exception e) {
				System.out.println("--warning...");
				e.printStackTrace();
			}

			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} // end big whill
			// finally {
			// try {
			// word_ActiveXComponent.invoke("Quit", new Variant[] {});
			// powerpoint_ActiveXComponent.invoke("Quit", new Variant[] {});
			// } catch (Exception e) {
			// System.out.println("--warning...");
			// e.printStackTrace();
			// }
			//
			//
			// }

	}

 





	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//WordServer.getInstance().run();
		new WordServer().run();
		System.out.println("  main finish ..");

	}

}
